/*******************************************************************************
 * Copyright (c) 2012 Alexandr Tsvetkov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Alexandr Tsvetkov - initial API and implementation
 * 
 * Project:
 *     TAO Library
 * 
 * File name:
 *     ImageListAdapter.java
 *     
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 ******************************************************************************/
package ua.at.tsvetkov.ui.menus;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

	/**
	 * Адаптер для списков с изображением и текстом
	 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
	 *
	 */
	public class ImageListAdapter extends BaseAdapter {
		private LayoutInflater	mInflater;
		private ArrayList<ImageItem> items;
		private int	layoutId;
		private int	imageViewId;
		private int	textViewId;

		/**
		 * Конструктор адаптера.
		 * @param context контекст
		 * @param layoutId id лэйаута в классе R
		 * @param imageViewId id изображения в указанном лэйауте
		 * @param textViewId id подписи в указанном лэйауте
		 * @param items подготовленный массив пунктов меню
		 */
		public ImageListAdapter(Context context, int layoutId, int imageViewId, int textViewId, ArrayList<ImageItem> items) {
			mInflater = LayoutInflater.from(context);
			if (items == null) {
				items = new ArrayList<ImageItem>();
			} else {
			this.items = items;
			this.layoutId = layoutId;
			this.imageViewId = imageViewId;
			this.textViewId = textViewId;
			}
		}
		
		/**
		 * Конструктор адаптера.
		 * @param context контекст
		 * @param layoutId id лэйаута в классе R
		 * @param imageViewId id изображения в указанном лэйауте
		 * @param textViewId id подписи в указанном лэйауте
		 */
		public ImageListAdapter(Context context, int layoutId, int imageViewId, int textViewId) {
			mInflater = LayoutInflater.from(context);
			items = new ArrayList<ImageItem>();
		}
		
		/**
		 * Добавление пункта меню с картинкой и подписью
		 * @param text
		 * @param id
		 */
		public void addItem(String text, int id) {
			items.add(new ImageItem(text, id));
		}

		public int getCount() {
			return items.size();
		}

		public Object getItem(int position) {
			return items.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = new ViewHolder();
			if (convertView == null) {
				convertView = mInflater.inflate(layoutId, null);
				holder.text = (TextView) convertView.findViewById(textViewId);
				holder.image = (ImageView) convertView.findViewById(imageViewId);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.text.setText(items.get(position).str);
			holder.image.setBackgroundResource(items.get(position).id);
			return convertView;
		}

	}

	class ViewHolder {
		TextView	text;
		ImageView image;
	}
